package com.moon.game.command.guest.hunt;

import java.util.ArrayList;
import java.util.Map;

import com.moon.game.clazz.PlayerItemsProto.PlayerItems.PlayerItem;
import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.exception.GException;
import com.moon.game.manager.ItemMG;
import com.moon.game.manager.LevelExpMG;
import com.moon.game.manager.PlayerMG;
import com.moon.game.message.ObjMessage;
import com.moon.game.resource.model.Item;
import com.moon.game.resource.model.Monster;
import com.moon.game.service.FightService;
import com.moon.game.service.ItemService;
import com.moon.game.service.MissionService;
import com.moon.game.service.PlayerItemService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.FightResult;
import com.moon.game.vos.PlayerItemVo;
import com.moon.game.vos.PlayerVo;

/**
 * @description 打怪
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-8 上午11:30:17
 */
public class HuntMonsterCmd extends BaseCmd{
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
		default:
			break;
		}
	}
	
	@Override
	protected void doResponse(RequestSession req) throws GException {
		ObjMessage resp = req.getClientResponse();
		PlayerVo player = PlayerMG.instance.getPlayer(req.getPlayerId());
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 1) {
			doResponsePsError(req);
			return;
		}
		String monsterId = ps[0];
		Monster monster = FightService.doFindMonsterById(player.getLocation(), monsterId);
		req.setVO("the_monster", monster);
		FightResult result = FightService.doFightMonster(player, monsterId);
		req.setVO("fight_result", result);
		req.setVO(PAR_PLAYER, player);
		if (result.getStatus() == Constants.FIGHT_NORMAL) {
			if (result.getSrcCurHp() <= 0) {
				//玩家战败
				player.setLocation(Constants.MAIN_CITY);
				int exp = (int) (LevelExpMG.instance.getLevelForExp(player.getLevel()) * 0.1);
				PlayerService.doAddExp(player.getSid(), player.getId(), -exp);
			}else if (result.getDstCurHp() <= 0) {
				//玩家胜利
				int exp = monster.getExp();
				PlayerService.doAddExp(player.getSid(), player.getId(), exp);
				int money = result.getMoney();
				PlayerService.doAddMoney(player.getSid(), player.getId(), money);
				//掉落物品
				Map<Integer, Integer> dropedMap = result.getItems();
				
				ArrayList<Integer> ids = new ArrayList<Integer>();
				ArrayList<Integer> nums = new ArrayList<Integer>();
				ArrayList<Item> items = new ArrayList<Item>();
				for (Map.Entry<Integer, Integer> entry : dropedMap.entrySet()) {
					//一个怪物只让有掉出的第一把武器添加给玩家
					final Item item = ItemMG.instance.getItem(entry.getKey());
					if (item.getPosition() > 0) {
						PlayerItem playerItem = PlayerItemService.playerItemFactory(item.getId());
						int ret = PlayerItemService.addPlayerItem(player.getSid(), player.getId(), playerItem, true);
						if (ret == Constants.ADD_ITEM_NO_BURDEN) {
							//负重不够无法获取掉落的装备
							req.setVO("no_burden", true);
						}
						PlayerItemVo vo = PlayerItemService.transDb2Vo(playerItem);
						req.setVO("player_item", vo);
						continue;
					}
					ids.add(entry.getKey());
					nums.add(entry.getValue());
					items.add(item);
				}
				
				int ret = ItemService.addItems(player,ids,nums,true);
				if (ret == Constants.ADD_ITEM_NO_ROOM) {
					req.setVO("no_room", true);
				}else if (ret == Constants.ADD_ITEM_FAIL) {
					req.setVO("fail_save_item", true);
				}
				
				//打怪任务
				MissionService.doHuntMonsterMission(player, monster.getId());
				
				req.setVO("items", items);
				req.setVO("exp", exp);
				req.setVO("money", money);
				req.setVO("drop_map", dropedMap);
				FightService.removeTheMonster(player.getLocation(), monsterId);
			}
		}
		response(req, resp);
	}

}
